﻿Imports System.IO
Imports System.IO.Ports.SerialPort

Public Class NOVA

#Region "Structures"
    Public Structure ResultStruct
        Dim TestCode As String
        Public DataValue As String
        Public Unit As String
        Dim DateTestCompleted As String
    End Structure
    Public Structure Patient
        Dim PatientName() As String
        Dim Age As String
        Dim Sex As String
        Dim DateOfBirth As String
        Dim PhysicianID As String
        Dim Location As String
        Dim PID As String
        Dim SID As String
        Dim ResultArray() As ResultStruct
        Dim Comment As String
    End Structure
#End Region
    Private Const ComponentDelimiter As Char = "^"
    Private Const RecordDelimiter As Char = vbCr
    Private Const FrameDelimiter As Char = vbCrLf
    Private Const FieldDelimiter As Char = "|"
    Private Const RepeatDelimiter As String = "\"
    Public Const ETB As Byte = &H17
    Public Const OrderRecordType As String = "O"
    Private Const PatientRecordType As String = "P"
    Private Const ResultRecordType As String = "R"

    Public Function FrameCheck(ByVal sFrameInput As String, ByRef FinalFrame As Boolean, ByRef sDataSegmentOutput As String) As Boolean
        Dim checksum1, checksum2 As Byte
        Dim sTemp As String
        checksum1 = Convert.ToByte(sFrameInput.Substring(sFrameInput.Length - 2), 16)
        sTemp = sFrameInput.Remove(sFrameInput.Length - 2)
        sTemp = sTemp.Substring(1)
        checksum2 = StringChecksum(sTemp)
        If checksum2 <> checksum1 Then
            Return False
        Else
            sDataSegmentOutput = sTemp.Remove(sTemp.Length - 1)     'Loc bo ETX hoac ETB
            sDataSegmentOutput = sDataSegmentOutput.Substring(1)            'Loc bo Frame No.
            If Asc(Convert.ToChar(sTemp.Substring(sTemp.Length - 1))) = ETB Then
                FinalFrame = False
            Else
                FinalFrame = True
            End If
            Return True
        End If
    End Function

    Public Function GetResult(ByVal sDataSegment As String, ByRef sRecordOutput() As String) As Boolean
        Dim sTemp1 As String()
        Dim s As String
        Dim NotResult As Boolean = True
        sRecordOutput = sDataSegment.Split(RecordDelimiter)
        For Each s In sRecordOutput
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = ResultRecordType Then
                If NotResult Then
                    NotResult = False
                End If
                Exit For
            End If
        Next
        Return (Not NotResult)
    End Function
    Public Function GetTerminationStatus(ByVal sDataSegment As String) As Boolean
        Dim sRecordTemp As String()
        Dim NotEnd As Boolean = True
        Dim sTemp1 As String()
        sRecordTemp = sDataSegment.Split(RecordDelimiter)
        For Each s As String In sRecordTemp
            sTemp1 = s.Split(FieldDelimiter)
            If sTemp1(0) = "L" Then
                'If sTemp1(2).Length > 0 And sTemp1(2) = "F" Then
                NotEnd = False
                Exit For
                'End If
            End If
        Next
        Return (Not NotEnd)
    End Function

    Private Function StringChecksum(ByVal sInput As String) As Byte
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod &H100
        Next
        Return bChecksum
    End Function

    Public Sub ResultRecordParse(ByVal sInput() As String, ByRef UniversalTestID() As String, ByRef DataValue As String, ByRef Units As String, ByRef DateTestCompleted As String)
        UniversalTestID = sInput(2).Split(ComponentDelimiter)
        DataValue = sInput(3)
        Units = sInput(4)
        DateTestCompleted = sInput(11)
    End Sub
    
    Public Sub PatientRecordParse(ByVal sInput() As String, ByRef PID As String)
        PID = sInput(3)
    End Sub
    Public Sub ResultDataParse(ByVal sInput() As String, ByRef PatientStructure As Patient)
        'Dim sTemp() As String = sInput.Split(RecordDelimiter)


        Dim sTemp2() As String = {}
        Dim s() As String
        Dim NoOfTest As Byte = 0
        Dim Aspect As Byte = 0
        Dim UniversalTestID() As String = {}
        Dim DataValue As String = [String].Empty
        Dim Units As String = [String].Empty
        Dim AllergyClassRange As String = [String].Empty
        Dim AbnormalFlags As String = [String].Empty
        Dim ResultStatus As String = [String].Empty
        Dim DateTestCompleted As String = [String].Empty
        With PatientStructure
            For i As Byte = 0 To sInput.GetLength(0) - 1
                s = sInput(i).Split(FieldDelimiter)
                Select Case s(0)
                    Case PatientRecordType
                        PatientRecordParse(s, .SID)
                    Case ResultRecordType
                        ResultRecordParse(s, UniversalTestID, DataValue, Units, DateTestCompleted)
                        NoOfTest += 1
                        ReDim Preserve PatientStructure.ResultArray(NoOfTest - 1)
                        With PatientStructure.ResultArray(NoOfTest - 1)
                            .TestCode = UniversalTestID(3)
                            .DataValue = DataValue
                            .Unit = Units
                            If DateTestCompleted.Length > 0 Then
                                .DateTestCompleted = DateTestCompleted
                            Else
                                .DateTestCompleted = PatientStructure.ResultArray(0).DateTestCompleted
                            End If
                        End With
                End Select
            Next
        End With

    End Sub

End Class